Loop_Practise_Que_12

Home / Java Practice Question / Loop_Practise_Que_12

Loop_Practise_Que_12


Write a Java program that displays the sum of n odd natural numbers.

Program : 


public class Loop {
public static int oddNatural(int n){
int sum=0;

for(int i=1;i<=n;i++){
if(i%2==1){
sum+=i;
}
}
return sum;
}
}